/**
 * 基本信息标签页相关代码
 */
var gwxx = (function() {

	var customerId = qmsk.getQueryValue("customerId");
	var shopId = 280000022;
	var $saveButton = $("<div class='BottomBtn'><input type='button' value='保存信息'></div>");

	var cache = {};

	function clearInput(id) {
		$("#" + id).find("input[type='text']").val('');
		$("#" + id).find("input[type='checkbox']").val('');
		$("#" + id).find("input[type='radio']").val('');
		$("#" + id).find("select").val('');
		$("#" + id).find("textarea").val('');
	}

	function disabledInput(id) {
		$("#" + id).find("input[type='text']").attr('disabled', 'disabled');
		$("#" + id).find("input[type='checkbox']").attr('disabled', 'disabled');
		$("#" + id).find("input[type='radio']").attr('disabled', 'disabled');
		$("#" + id).find("select").attr('disabled', 'disabled');
		$("#" + id).find("textarea").attr('disabled', 'disabled');
	}

	function removeDisabledInput(id) {
		$("#" + id).find("input[type='text']").removeAttr('disabled');
		$("#" + id).find("input[type='checkbox']").removeAttr('disabled');
		$("#" + id).find("input[type='radio']").removeAttr('disabled');
		$("#" + id).find("select").removeAttr('disabled');
		$("#" + id).find("textarea").removeAttr('disabled');
	}

	/*
	 * 页面加载的时候进行的初始化
	 */
	function loadInit() {
		disabledInput("gw_gwxx");
		clearInput("gw_gwxx");

		$("#gw_gwxx_typeId").bind('change', function() {
			$("#gw_gwxx_advisorName").val('');
		});

		$("#gw_add").unbind().bind('click', function() {
			clearInput("gw_gwxx");
			removeDisabledInput("gw_gwxx");
			$saveButton.remove();
			$("#gw_gwxx").append($saveButton);
			cache.userId = '';
			$saveButton.unbind().bind('click', insert);
		});
		loadSimpleList();

	}

	function insert() {

		var userId = cache.userId;
		if (qmsk.isEmpty(userId)) {
			artAlert("请选择顾问");
			return;
		}
		var typeId = $("#gw_gwxx_typeId").val();

		qmsk.post('../myAdvisor/insert/any/' + typeId + "/" + shopId + "/" + customerId + "/" + cache.userId, function(response) {

			var id = response.getValue("id");
			if (qmsk.isEmpty(id)) {
				artError("添加失败！");
				return;
			}
			cache.id = id;
			artSuccess('添加成功');
			$saveButton.remove();
			disabledInput("gw_gwxx");
			loadSimpleList();
		});
	}

	function loadSimpleList() {

		qmsk.get('../myAdvisor/list/' + customerId + '/0/99', function(response) {
			cache.list = response;
			qmsk.dataTable({
				tbodyId : 'gw_list',
				heads : ["typeName", "userName", "id"],
				response : response,
				sup : ['gw_list_sup', '共 [index] 条'],
				handler : [[2, detailButton]]
			});
		});

		var detailButton = function(id, row) {
			var detail = "<input onclick='_loadgwxxDetail(" + row + ")' type='button' class='common_btn' value='查看详情'>";
			var del = "<input onclick='_delgwxxDetail(" + id + ")' type='button' class='common_btn del' value='删除'>";
			return detail + del;
		};
	}


	window._loadgwxxDetail = loadgwxxDetail;

	function loadgwxxDetail(row) {
		$saveButton.remove();
		var response = cache.list;

		var id = response.getValue("id", row);
		var type = response.getValue('type', row);
		var userId = response.getValue('userId', row);
		var userName = response.getValue('userName', row);

		cache.id = id;
		cache.userId = userId;
		cache.type = type;

		$("#gw_gwxx_typeId").val(type);
		$("#gw_gwxx_advisorName").val(userName);

		disabledInput("gw_gwxx");
		$("#gw_gwxx_edit").unbind().bind('click', function() {
			removeDisabledInput("gw_gwxx");
			$("#gw_gwxx_typeId").attr('disabled', 'disabled');
			$saveButton.remove();
			$("#gw_gwxx").append($saveButton);
			$saveButton.unbind().bind('click', update);
		});

	}

	function _delgwxxDetail(id) {
		artConfirm('确定要删除吗？', function() {
			qmsk.post('../myAdvisor/delete/' + id, function() {
				artSuccess('删除成功');
				loadSimpleList();
			});
		});
	}


	window._delgwxxDetail = _delgwxxDetail;

	function update() {
		var id = cache.id;
		var userId = cache.userId;
		qmsk.post('../myAdvisor/update/' + id + '/' + userId, function() {
			artSuccess('更新成功!');
			$saveButton.remove();
			disabledInput("gw_gwxx");
			loadSimpleList();
		});
	}

	function gwSelecter(self) {

		var typeName = $("#gw_gwxx_typeId > option:selected").text();

		$(self).blur();
		initSelecter({
			method : 'post',
			url : '../user/find/userSelecter',
			data : {
				dutyName : typeName
			},
			heads : ["customerName", "loginName"],
			titles : ["用户名称", "登录名称"],
			zindex : 500,
			callBack : function(response, row) {
				var id = response.getValue('customerId', row);
				var name = response.getValue('customerName', row);
				$(self).val(name);
				cache.userId = id;

			}
		});
	}


	window.gwSelecter = gwSelecter;

	return {
		init : function() {
			loadInit();
		}
	};
})();
